package top.hkyzf.study.j2se.juc;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.util.concurrent.CountDownLatch;

/**
 * 秦灭六国，班长锁门
 * @author zhufeng
 * @version 1.0
 * @date 2021-3-10 23:07
 */
public class CountDownLatchDemo {
    public static void main(String[] args) {
        CountDownLatch countDownLatch = new CountDownLatch(6);

        for (int i = 0; i < 6; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + " 国被灭！");
                countDownLatch.countDown();
            }, Country.forEach(i+1).getName()).start();
        }

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("秦国一统华夏");
    }
}

enum Country {
    ONE(1, "齐"), TWO(2, "楚"), THREE(3, "燕"), FOUR(4, "赵"), FIVE(5, "魏"), SIX(6, "韩");

    private Integer code;
    private String name;

    Country(Integer code, String name) {
        this.code = code;
        this.name = name;
    }

    public Integer getCode() {
        return code;
    }

    public String getName() {
        return name;
    }

    public static Country forEach(int code) {
        Country[] values = Country.values();
        for(Country value : values) {
            if(value.getCode() == code) {
                return value;
            }
        }
        return null;
    }
}
